package com.bjpowernode.money.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.bjpowernode.money.PageModel;
import com.bjpowernode.money.mapper.LoanInfoMapper;
import com.bjpowernode.money.model.LoanInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service(interfaceClass = LoanInfoService.class,version = "1.0.0",timeout = 20000)
@Component
public class LoanInfoServiceImpl implements LoanInfoService {

    @Autowired
    LoanInfoMapper loanInfoMapper;
    @Autowired
    RedisTemplate redisTemplate;

    //在首页显示 历史年收益率 (要放在Redis中)
    @Override
    public Double queryLoanInfoHistoryRateAvg() {

        //设置redisTemplate对象key的序列化方式  使在存存储到redis中时名字
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //从redis缓存中获取历史年化收益率  opsForValue()针对字符串类型
        Double loanInfoHistoryRateAvg = (Double) redisTemplate.opsForValue().get("loanInfoHistoryRateAvg");

        //判断是否为空
        if(loanInfoHistoryRateAvg==null){
            //加锁
            synchronized (this){
                //双重检验 防止缓存穿透
                loanInfoHistoryRateAvg = (Double) redisTemplate.opsForValue().get("loanInfoHistoryRateAvg");
                if(loanInfoHistoryRateAvg==null){
                    loanInfoHistoryRateAvg = loanInfoMapper.selectLoanInfoHistoryRateAvg();
                    redisTemplate.opsForValue().set("loanInfoHistoryRateAvg",loanInfoHistoryRateAvg,15, TimeUnit.SECONDS);
                }
            }
        }

        return loanInfoHistoryRateAvg;
    }

    //在首页显示 新手宝、季度宝、散标并显示在页面上
    @Override
    public List<LoanInfo> queryLoanInfosByTypeAndNumber(Map<String, Object> parasMap) {

        return loanInfoMapper.selectLoanInfosByTypeAndNumber(parasMap);

    }

    //列表页面 分页显示散标的产品信息
    @Override
    public List<LoanInfo> queryLoanInfosByTypeAndPageModel(Integer ptype, PageModel pageModel) {

        /**
         *   在数据库中进行查询 sql语句中的参数有
         *   数据库要分页 要有起始的行数 start  每页显示多少个 content
         *    1  0-9   (1-1)*10  0
         *    2 10-19  (2-1)*10  10
         *    3 20-29
         *
         *    limit start,content   limit 开始行数，显示多少个
         *
         *    start = 当前页-1 乘以 每页的记录数
         *    content = 每页的记录数
         */

        Long start = (pageModel.getCunPage() - 1)*pageModel.getPageSize();
        Integer content = pageModel.getPageSize();

        Map<String,Object> parasMap = new HashMap<>();
        parasMap.put("start",start);
        parasMap.put("content",content);
        parasMap.put("ptype",ptype);

        return loanInfoMapper.selectLoanInfosByTypeAndPageModel(parasMap);
    }

    //列表页面 根据类型查找该类型对应的所有产品个数
    @Override
    public Long queryLoanInfoCountByType(Integer ptype) {

        return loanInfoMapper.selectLoanInfoCountByType(ptype);
    }

    //产品详情页面   根据id查询对应的产品信息
    @Override
    public LoanInfo queryLoanInfoById(int loanId) {

        return loanInfoMapper.selectByPrimaryKey(loanId);
    }
}
